<template>
	<view class="main">
		<view>
			<u--form labelPosition="left" :model="formData" :rules="rules" ref="form" labelWidth='180' labelAlign='left'
				:labelStyle="{marginLeft: '10px',fontSize: '30rpx',color: '#1D2129',height:'96rpx',lineHeight:'96rpx'}"
				:borderBottom="true">
				<u-form-item label="房车名称" prop="rvInfo" required borderBottom class='formItemWp'>
					<u--input v-model="formData.rvInfo" border="none" placeholder='请输入房车名称'>
					</u--input>
				</u-form-item>
				<u-form-item label="设备编号" prop="equipmentCode" required borderBottom class='formItemWp'>
					<u--input v-model="formData.equipmentCode" :readonly="itemId == '' ? false : true" border="none"
						placeholder='请输入设备编号'>
					</u--input>
				</u-form-item>
				<u-form-item label="价格" prop="price" required borderBottom class='formItemWp'>
					<u--input v-model="formData.price" border="none" placeholder='请输入价格'>
					</u--input>
				</u-form-item>
			</u--form>
		</view>
		<view class="submitWp bottomBar">
			<view class="submitBtn">
				<u-button @click="submit" :throttleTime="1000" color="#276aee">保存</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					rvInfo: '',
					equipmentCode: '',
					price: '',
				},
				openId:'',
				itemId: '',
				rules: {
					rvInfo: [{
						required: true,
						message: '请输入房车名称',
						trigger: ['blur'],
					}, ],
					equipmentCode: [{
						required: true,
						message: '请输入设备编号',
						trigger: ['blur'],
					}],
					price: [{
						required: true,
						message: '请输入价格',
						trigger: ['blur'],
					}],
				},
			}
		},
		onLoad(e) {
			if (e.id) {
				this.itemId = e.id
				this.queryById();
			}
			let data = uni.getStorageSync('openId')
			if (data.openid) {
				this.openId = data.openid
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			async queryById() {
				let res = await this.$api.queryByIdRv({
					id: this.itemId
				})
				this.formData = res.result
				this.formData.price = res.result.price + ''
			},
			async submit() {
				this.$refs.form.validate().then(async res => {
					if (this.itemId) {
						this.formData.id = this.itemId
						let res1 = await this.$api.editRv(this.formData)
						if (res1.success) {
							uni.navigateBack()
						}
						uni.$u.toast(res1.message)
					} else {
						this.formData.openid = this.openId
						let res1 = await this.$api.addRv(this.formData)
						if (res1.success) {
							uni.navigateBack()
						}
						uni.$u.toast(res1.message)
					}
				}).catch(errors => {
					console.log(errors);
					uni.$u.toast('内容填写有误,请修改或补充')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.formItemWp {
		margin: 0 50upx;
		height: 108rpx;
		border-bottom: #666;
	}

	.submitWp {
		z-index: 9999;
		width: 100%;
		background-color: #fff;
		position: fixed;
		bottom: 0rpx;
		box-sizing: border-box;

		.submitBtn {
			margin: 30rpx 50rpx;
		}
	}

	::v-deep .u-form-item__body__left__content__label {
		font-weight: 600 !important;
	}
</style>
